package com.example.service.Impl;

import com.example.Constants.SystemConstants;
import com.example.domain.entity.LoginUser;
import com.example.domain.entity.User;
import com.example.enums.AppHttpCodeEnum;
import com.example.handler.exception.SystemException;
import com.example.mapper.InitMapper;
import com.example.mapper.MenuMapper;
import com.example.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private InitMapper initMapper;
    @Override
    public UserDetails loadUserByUsername(String studentid) throws UsernameNotFoundException {
        User user = userMapper.selectByStudentId(studentid);
        if (Objects.isNull(user)){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_ERROR);
        }
        // 封装权限信息
        List<String> perms;
        if (SystemConstants.ADMIN.equals(user.getAuthority())) {
            //当前用户为超级管理员
            perms = initMapper.selectAllMenuPermS();
        }else {
            //当前用户为非超级管理员
            perms = initMapper.selectMenuPremsByUserId(user.getId());
        }
        return new LoginUser(user,perms);
    }
}
